package cn.snail.app.clockwidget;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RemoteViews;

public class DigitalClockWidget extends AppWidgetProvider {
	public static final String TAG = "DigitalClockWidget";
	
	private Context mContext;
	private AppWidgetManager mAppWigetManager;
	private Handler handler = new Handler() {
	private String minute_r_1,minute_r,minute_l_1,minute_l,hour_1,hour = "0000";
		public void handleMessage(Message msg) {
			//if (msg.what == 0x1) {
				Date date = new Date();
				
				

				minute_r_1 = Integer.toBinaryString(date.getMinutes()%10);
				minute_r = "minute_r";
				changelight(minute_r_1,minute_r);
				minute_l_1 = Integer.toBinaryString(date.getMinutes()/10);
				minute_l = "minute_l";
				changelight(minute_l_1,minute_l);
				
				hour_1 = Integer.toBinaryString(date.getHours()%10);
				hour = "hour_r";
				changelight(hour_1,hour);
				hour_1 = Integer.toBinaryString(date.getHours()/10);
				hour = "hour_l";
				changelight(hour_1,hour);
				
				
				//Log.i(TAG, "...zhangwei"+minute_r_1);
				
				
				//mAppWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
			//}
		}
	};
	
	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i(TAG, "...zhangweionReceive");
		super.onReceive(context, intent);
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		Log.i(TAG, "...zhangweionUpdate");
		this.mContext = context;
		this.mAppWigetManager = appWidgetManager;
		
		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				handler.sendEmptyMessage(0x1);
			}
			
		}, 0, 60000);

		//super.onUpdate(context, appWidgetManager, appWidgetIds);
	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		Log.i(TAG, "...zhangweionDeleted");
		super.onDeleted(context, appWidgetIds);
	}

	@Override
	public void onEnabled(Context context) {
		Log.i(TAG, "...zhangweionEnabled");
		super.onEnabled(context);
	}

	@Override
	public void onDisabled(Context context) {
		Log.i(TAG, "...zhangweionDisabled");
		super.onDisabled(context);
	}
	
	public void changelight(String tm,String hourminute){ 
		RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.digital_clock);
		switch (tm.length()){
		case 1:
			tm = "000"+tm;
			break;
		case 2:
			tm = "00"+tm;
			break;
		case 3:
			tm = "0"+tm;
			break;
	}
	if(tm.substring(0, 1).equals("1")){
		if(hourminute=="minute_r"){
			remoteViews.setImageViewResource(R.id.minute_r_4, R.drawable.light);
		}else if(hourminute=="minute_l"){
			remoteViews.setImageViewResource(R.id.minute_l_4, R.drawable.light);
		}else if(hourminute=="hour_r"){
			remoteViews.setImageViewResource(R.id.hour_r_4, R.drawable.light);
		}else if(hourminute=="hour_l"){
			remoteViews.setImageViewResource(R.id.hour_l_4, R.drawable.light);
		}
	}else{
		if(hourminute=="minute_r"){
			remoteViews.setImageViewResource(R.id.minute_r_4, R.drawable.dark);
		}else if(hourminute=="minute_l"){
			remoteViews.setImageViewResource(R.id.minute_l_4, R.drawable.dark);
		}else if(hourminute=="hour_r"){
			remoteViews.setImageViewResource(R.id.hour_r_4, R.drawable.dark);
		}else if(hourminute=="hour_l"){
			remoteViews.setImageViewResource(R.id.hour_l_4, R.drawable.dark);
		}
	}
	if(tm.substring(1, 2).equals("1")){
		if(hourminute=="minute_r"){
			remoteViews.setImageViewResource(R.id.minute_r_3, R.drawable.light);
		}else if(hourminute=="minute_l"){
			remoteViews.setImageViewResource(R.id.minute_l_3, R.drawable.light);
		}else if(hourminute=="hour_r"){
			remoteViews.setImageViewResource(R.id.hour_r_3, R.drawable.light);
		}else if(hourminute=="hour_l"){
			remoteViews.setImageViewResource(R.id.hour_l_3, R.drawable.light);
		}
	}else{
		if(hourminute=="minute_r"){
			remoteViews.setImageViewResource(R.id.minute_r_3, R.drawable.dark);
		}else if(hourminute=="minute_l"){
			remoteViews.setImageViewResource(R.id.minute_l_3, R.drawable.dark);
		}else if(hourminute=="hour_r"){
			remoteViews.setImageViewResource(R.id.hour_r_3, R.drawable.dark);
		}else if(hourminute=="hour_l"){
			remoteViews.setImageViewResource(R.id.hour_l_3, R.drawable.dark);
		}
	}
	if(tm.substring(2, 3).equals("1")){
		if(hourminute=="minute_r"){
			remoteViews.setImageViewResource(R.id.minute_r_2, R.drawable.light);
		}else if(hourminute=="minute_l"){
			remoteViews.setImageViewResource(R.id.minute_l_2, R.drawable.light);
		}else if(hourminute=="hour_r"){
			remoteViews.setImageViewResource(R.id.hour_r_2, R.drawable.light);
		}else if(hourminute=="hour_l"){
			remoteViews.setImageViewResource(R.id.hour_l_2, R.drawable.light);
		}
	}else{
		if(hourminute=="minute_r"){
			remoteViews.setImageViewResource(R.id.minute_r_2, R.drawable.dark);
		}else if(hourminute=="minute_l"){
			remoteViews.setImageViewResource(R.id.minute_l_2, R.drawable.dark);
		}else if(hourminute=="hour_r"){
			remoteViews.setImageViewResource(R.id.hour_r_2, R.drawable.dark);
		}else if(hourminute=="hour_l"){
			remoteViews.setImageViewResource(R.id.hour_l_2, R.drawable.dark);
		}
	}
	if(tm.substring(3, 4).equals("1")){
		if(hourminute=="minute_r"){
			remoteViews.setImageViewResource(R.id.minute_r_1, R.drawable.light);
		}else if(hourminute=="minute_l"){
			remoteViews.setImageViewResource(R.id.minute_l_1, R.drawable.light);
		}else if(hourminute=="hour_r"){
			remoteViews.setImageViewResource(R.id.hour_r_1, R.drawable.light);
		}else if(hourminute=="hour_l"){
			remoteViews.setImageViewResource(R.id.hour_l_1, R.drawable.light);
		}
	}else{
		if(hourminute=="minute_r"){
			remoteViews.setImageViewResource(R.id.minute_r_1, R.drawable.dark);
		}else if(hourminute=="minute_l"){
			remoteViews.setImageViewResource(R.id.minute_l_1, R.drawable.dark);
		}else if(hourminute=="hour_r"){
			remoteViews.setImageViewResource(R.id.hour_r_1, R.drawable.dark);
		}else if(hourminute=="hour_l"){
			remoteViews.setImageViewResource(R.id.hour_l_1, R.drawable.dark);
		}
	}
	
	ComponentName componentName = new ComponentName(mContext, DigitalClockWidget.class);
	mAppWigetManager.updateAppWidget(componentName, remoteViews);
	}

}
